package com.easepal7506a.project.widget;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.easepal7506a.project.Constant.CommmandNum;
import com.easepal7506a.project.R;
import com.easepal7506a.project.interfaces.IProMasaChoose;
import com.example.reslib.utils.PreferenceWrapper;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 * 主题模式
 * Created by FHB on 2018/3/26.
 */

public class LayoutZtms_7506 extends RelativeLayout implements View.OnClickListener {
    private ImageView mIvWorker;//上班族
    private ImageView mIvLower;//低头族
    private ImageView mIvDriver;//开车族
    private TextView mTvDriver;//开车族
    private ImageView mIvShose;//高跟族
    private List<ImageView> mViewList = new ArrayList<>();
    private IProMasaChoose mAction;
    private MyHandler myHandler;

    public LayoutZtms_7506(Context context) {
        super(context);
    }

    public LayoutZtms_7506(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setAction(IProMasaChoose action) {
        mAction = action;
    }


    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        myHandler = new MyHandler(this);
        mIvWorker = findViewById(R.id.zy_masa_iv_sbz);
        mIvLower = findViewById(R.id.zy_masa_iv_dtz);
        mIvDriver = findViewById(R.id.zy_masa_iv_jzc);
        mTvDriver = findViewById(R.id.zy_masa_tv_jzc);
        PreferenceWrapper mSp = new PreferenceWrapper();
        int mChoose = Integer.parseInt(mSp.getStringValue("app", "1"));
        mTvDriver.setText(mChoose == 4 ? R.string.zy_masa_jcz_7507 : R.string.zy_masa_jcz);
        mIvShose = findViewById(R.id.zy_masa_iv_ggz);
        mViewList.add(mIvWorker);
        mViewList.add(mIvLower);
        mViewList.add(mIvDriver);
        mViewList.add(mIvShose);
        mIvWorker.setOnClickListener(this);
        mIvLower.setOnClickListener(this);
        mIvDriver.setOnClickListener(this);
        mIvShose.setOnClickListener(this);
    }

    public void setSelect(int position) {
        myHandler.obtainMessage(100, position).sendToTarget();

    }

    private void makeChoose(int position) {
        if (position == -1) {
            for (int i = 0; i < mViewList.size(); i++) {
                mViewList.get(i).setSelected(false);
            }
        } else {
            for (int i = 0; i < mViewList.size(); i++) {
                if (i == position) {
                    Log.e("getDataPer2", "select" + position);
                    mViewList.get(i).setSelected(true);
                } else {

                    mViewList.get(i).setSelected(false);
                }

            }
        }
    }

    private class MyHandler extends Handler {
        WeakReference<LayoutZtms_7506> mMainActivityWeakReference;

        MyHandler(LayoutZtms_7506 layoutOne) {
            mMainActivityWeakReference = new WeakReference<>(layoutOne);
        }

        @Override
        public void handleMessage(Message msg) {
            final LayoutZtms_7506 mLayoutOne = mMainActivityWeakReference.get();
            int position = (int) msg.obj;
            Log.e("getDataPer", "select" + position);
            if (mLayoutOne != null) {

                makeChoose(position);
            }
        }
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.zy_masa_iv_sbz) {
            mAction.chooseMassage("上班族", CommmandNum.SHANGBZ);

        } else if (i == R.id.zy_masa_iv_dtz) {
            mAction.chooseMassage("低头族", CommmandNum.DiTZ);

        } else if (i == R.id.zy_masa_iv_jzc) {
            mAction.chooseMassage("开车族", CommmandNum.JIACZ);

        } else if (i == R.id.zy_masa_iv_ggz) {
            mAction.chooseMassage("高跟族", CommmandNum.GAOGZ);

        }
    }
}
